/**
 * @param {number} n
 * @param {number} k
 * @return {number[][]}
 * 输入：n = 4, k = 2
输出：
[
  [2,4],
  [3,4],
  [2,3],
  [1,2],
  [1,3],
  [1,4],
]
 */
var combine = function (n, k) {
  const res = []
  const arr = new Array(n).fill(0).map((_, index) => index + 1)
  if (k === 1) {
    while (arr.length) {
      res.push([arr.pop()])
    }
    return res
  }
  const getNumCombine = (tarr, resArr) => {
    let i = 0
    if (resArr.length === k) {
      res.push(resArr)
      return
    }
    const arr = [...tarr]
    while (i < tarr.length && resArr.length !== k) {
      const del = arr.splice(0, 1)
      getNumCombine(arr, [...resArr, del[0]])
      i++
    }
  }
  getNumCombine([...arr], [])
  return res
}
console.log(combine(4, 2))
